package com.tongzhou.client;

import java.io.UnsupportedEncodingException;
import java.util.List;

import net.sf.json.JSONObject;

import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.account.model.SearchAccountCondition;
import com.tongzhou.tongzhou.order.dao.IOrderDAO;
import com.tongzhou.tongzhou.order.dao.impl.OrderDAO;
import com.tongzhou.tongzhou.order.model.GoodsOrder;
import com.tongzhou.tongzhou.order.model.SearchOrderCondition;
import com.tongzhou.util.Util;

//物流查询
public class ClientQuery {
	IOrderDAO dao=new OrderDAO();
	private SearchOrderCondition condition=null;
	private int pageSize ;
	private int currentPage ;
	private String orderBy ;
	private String orderByType ;
	public String getInfo(String json){
		JSONObject jsonObject = JSONObject.fromObject( json );
		String companyNum=null;
		String companyName=null;
		Integer oid=null;
		Integer paperType=null;
		Integer degree=null;
		String date=null;
		try{
			if(jsonObject.get("companyNum")!=null){
				companyNum=(String) jsonObject.get("companyNum");
			}
			
			if(jsonObject.get("companyName")!=null){
				companyName=(String) jsonObject.get("companyName");
//				try {
//					companyName=new String(companyName.getBytes("ISO-8859-1"),"UTF-8");
//				} catch (UnsupportedEncodingException e) {
//					e.printStackTrace();
//				}
			}
			
			if(jsonObject.get("oid")!=null){
				oid=(Integer) jsonObject.get("oid");
			}
			
			if(jsonObject.get("paperType")!=null){
				paperType=(Integer) jsonObject.get("");
			}
			
			if(jsonObject.get("degree")!=null){
				degree=(Integer) jsonObject.get("degree");
			}
			
			if(jsonObject.get("date")!=null){
				date=(String) jsonObject.get("date");
			}
			
			if(jsonObject.get("page")!=null){
				currentPage=(Integer) jsonObject.get("page");
			}
			
			if(jsonObject.get("pageNum")!=null){
				pageSize=(Integer) jsonObject.get("pageNum");
			}
			
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		StringBuffer str=new StringBuffer();
		if(companyNum==null||companyName==null){
			return "{'success':0,'msg':'输入参数有误!'}";
		}else{
			GoodsOrder order=new GoodsOrder();
			order.setCompany_num(companyNum);
			order.setCompany_name(companyName);
			if(oid!=null){
				order.setId(oid);
			}
			if(paperType!=null){
				order.setPaper_id(paperType);
			}
			
			if(degree!=null){
				order.setDegree_id(degree);
			}
			if(date!=null){
				order.setFinish_date(date);
			}
			try {
				checkSearchCondition();
				condition = new SearchOrderCondition();
				if (currentPage == 0){
					currentPage = 1;
				}
				condition.setCurrentPage(currentPage) ;
				condition.setCompany_name(companyName);
				condition.setCompany_num(companyNum);
				condition.setOid(oid);
				condition.setPaper_id(paperType);
				condition.setDegree_id(degree);
				condition.setFinish_date(date);
				
				condition.setOrderBy(orderBy);
				condition.setOrderByType(orderByType) ;
				condition.setPageSize(pageSize);
				List<GoodsOrder> orderList=dao.getOrderListByPage(condition);
				if(orderList.size()>0){
					str.append("{'success':1,'msg':[");
					for(int i=0;i<orderList.size();i++){
						GoodsOrder o=orderList.get(i);
						str.append("{'oid':"+o.getId()+",");
						str.append("'status':'"+Util.changeToObj(o.getStatus())+"',");
						str.append("'paperType':"+o.getPaper_id()+",");
						str.append("'degree':'"+o.getDegree_id()+"'}");
						if(i<orderList.size()-1){
							str.append(",");
						}
					}
					str.append("]}");
				}else{
					str.append("{'success':0,'msg':'没有对应的物流信息!'}");
				}
				return str.toString();
			} catch (DBException e) {
				e.printStackTrace();
			}
		}
//		String str="{'success':1,'msg':[{'oid':123,'status':'良好','paperType':321,'degree':222},";
//		str+="{'oid':123,'status':'良好','paperType':321,'degree':222}]}";
		return "{'success':0,'msg':'输入参数有误!'}";
	}
	/**
	 * 设置列表显示格式
	 *@author: 彭定雄
	 */
	private void checkSearchCondition(){
		if(currentPage <=0){
			currentPage = 1 ;
		}
		
		if(pageSize<=0){
			pageSize = 10 ;
		}
		
		if(orderBy==null||"".equals(orderBy)){
			orderBy = "id" ;
		}
		
		if(orderByType == null || "".equals(orderByType)){
			orderByType = "desc" ;
		}
	}
}
